package org.lq.shopping.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.lq.shopping.dao.Shop_carDao;
import org.lq.shopping.dao.impl.Shop_carDaoImpl;
import org.lq.shopping.entity.Shop_car;
import org.lq.shopping.service.Shop_carService;

import com.google.gson.Gson;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
 * 
 * @author 王江超
 *
 * @时间2018年9月29日
 * EShop
 */
public class Shop_carServiceImpl implements Shop_carService {

	Shop_carDao dao = new  Shop_carDaoImpl();
                  
	@Override
	public boolean addGoods(Shop_car t) {
		
		return dao.add(t)>0;
	}

	@Override
	public boolean deleteGoods(int uid) {
		
		return dao.delete(uid)>0;
	}

	@Override
	public boolean updateGoods(Shop_car t) {

		return dao.update(t)>0;
	}

	@Override
	public List<Shop_car> getShopCarInfoByUid(int pageIndex, int pageSize, int uid) {
		List<Shop_car> list = dao.getShopCarInfoByUid(uid);
//		System.out.println(list);
		
		JedisPoolConfig config = new JedisPoolConfig();
		//控制最大连接数
		config.setMaxTotal(50);
		//设置空闲连接
		config.setMaxIdle(5);
		//默认-1,等待时间,如果等待时间内没有获得连接,之间抛出异常
		config.setMaxWaitMillis(1000*100);
		//在获取连接之前,validate操作,验证  保证得到连接可用
		config.setTestOnBorrow(true);
		//当返回一个连接pool 的时候先进行ping() 判断是否可以连接
		config.setTestOnReturn(true);
		//配置接口密码
		JedisPool pool = new  JedisPool(config,"127.0.0.1",6379,2000);
		//一个redis连接对对象
		Jedis jedis = pool.getResource();
		jedis.select(8);
		
		Shop_car sCar = new Shop_car();
		Gson gson = new Gson();
				
		if (jedis.keys("shop_car") == null) {
			for(Shop_car s :list) {
				String s3 =gson.toJson(s);
				jedis.lpush("shop_car", s3);
			}
		}
		
		
		List<String> list1 = jedis.lrange("shop_car", (pageIndex -1)*pageSize, pageIndex*pageSize);
//		System.out.println(list1);
//		String  rs = jedis.get("shop_car");
		List<Shop_car> shop_cars = new ArrayList<>();
		for (String str : list1) {
			Shop_car s = gson.fromJson(str,Shop_car.class);
//			System.out.println(s);
			shop_cars.add(s);
		}
		
		
		
		return shop_cars;
	}

	
}
